These conditional forms augment Emacs Lisp's
simple if, and, or, and
cond forms.
This macro evaluates keyform, then compares it with the key values listed in the various clauses. Whichever clause matches the key is executed; comparison is done by
eql. If no clause matches, thecaseform returnsnil. The clauses are of the form(keylist body-forms...)where keylist is a list of key values. If there is exactly one value, and it is not a cons cell or the symbol
nilort, then it can be used by itself as a keylist without being enclosed in a list. All key values in thecaseform must be distinct. The final clauses may usetin place of a keylist to indicate a default clause that should be taken if none of the other clauses match. (The symbolotherwiseis also recognized in place oft. To make a clause that matches the actual symbolt,nil, orotherwise, enclose the symbol in a list.)For example, this expression reads a keystroke, then does one of four things depending on whether it is an ‘a’, a ‘b’, a <RET> or C-j, or anything else.
(case (read-char) (?a (do-a-thing)) (?b (do-b-thing)) ((?\r ?\n) (do-ret-thing)) (t (do-other-thing)))
This macro is just like
case, except that if the key does not match any of the clauses, an error is signaled rather than simply returningnil.
This macro is a version of
casethat checks for types rather than values. Each clause is of the form ‘(type body...)’. See Type Predicates, for a description of type specifiers. For example,(typecase x (integer (munch-integer x)) (float (munch-float x)) (string (munch-integer (string-to-int x))) (t (munch-anything x)))The type specifier
tmatches any type of object; the wordotherwiseis also allowed. To make one clause match any of several types, use an(or ...)type specifier.